<?php
/**
 * Created by PhpStorm.
 * User: 7howe
 * DateTime: 2019/11/25 18:03
 */

namespace app\goods\validate;


use think\Validate;

class Comment extends Validate
{
    protected $rule = [
        'star|评分值'     => 'require|between:1,5',
        'type|是否匿名'   => ["in"=>[0,1]],
        'content|内容'    => ['require',"max"=>200],
        'thumb|图片'      => ["checkIds"=>3],
        'goods_id|商品ID' => ["require","integer","gt"=>0],
        'sku_id|规格ID'   => ["require","integer","egt"=>0],
        'order_sn|订单号'   => ["require"],
    ];

    protected $message = [
        'star.between' => '评分值必须为1~5',
        'type.in' => '是否匿名选择不正确',       
    ];
    
    public $scene = [
        "edit"=>["star","content","thumb"]
    ];




    /**    
     * Ids验证器
     * @author 晓风<215628355@qq.com>
     * @param string $value 值
     * @param int $rule 规则
     * @param array $data 数据
     * @param string $filed 字段
     * @return boolean
     */
    protected function checkIds($value,$rule,$data=[],$filed = ""){
        $arr = explode(",",$value);
        if(count($arr) > $rule){
            return  ":attribute最多可传". $rule. "张";
        }
        foreach($arr as $v){
            if(!preg_match( '/^\d+$/',$v)){
                return ":attribute只能是数字列表组成";
            }
        } 
        return true;
    }
}
